package v1

import (
	"auth/bll"
	"auth/model"
	"auth/utils"
	"github.com/gin-gonic/gin"
)

var Resource = &resource{}

type resource struct{}

func (a *resource) Init(r *gin.RouterGroup) {
	//g := r.Group("/resource", middleware.TokenCheck())
	g := r.Group("/resource")
	{
		g.POST("/create", a.create)
		g.POST("/update", a.update)
		g.POST("/list", a.list)
		g.POST("/delete", a.delete)

	}
}

func (a *resource) create(c *gin.Context) {
	var (
		in  = &model.ResourceCreateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Resource.Create(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

func (a *resource) update(c *gin.Context) {
	var (
		in  = &model.ResourceUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Resource.Update(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}

func (a *resource) list(c *gin.Context) {
	var (
		in  = &model.ResourceListRequest{}
		out = &model.ResourceListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.Resource.List(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, out)
}

func (a *resource) delete(c *gin.Context) {
	var (
		in  = &model.ResourceDeleteRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Resource.Delete(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOK(c, nil)
}
